<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>密码修改</title>
    <link rel="stylesheet" href="../../lib/layui/css/layui.css"/>
</head>
<body style="margin: 0px;padding: 0px;">
<div style="width: 100%;height: 100%;">
    <form  class="layui-from layui-form-pane"  style="width: 550px;height: 500px;margin: 50px auto;box-shadow: #e3e3e3 10px 10px 30px 5px;padding-top: 80px">
        <div style="margin: 0 auto;width: 450px;height: 60px;text-align: center">
            <h2 style="color: #121a65">密码修改</h2>
        </div>
        <div style="margin: 0 auto;width: 400px;height: 270px;">
            <div class="layui-form-item" style="margin-top: 30px">
                <label class="layui-form-label">原密码&#12288;</label>
                <div class="layui-input-block">
                    <input id="oldPwd" name="oldPwd" type="password" style="float: left;"
                           class="layui-input" placeholder="请输入原密码" lay-verify="required" autocomplete="off"></div>
            </div>
            <div class="layui-form-item" style="margin-top: 30px">
                <label class="layui-form-label">新密码&#12288;</label>
                <div class="layui-input-block">
                    <input id="newPwd" name="newPwd" type="password" style="float: left;"
                           class="layui-input" placeholder="请输入新密码"  lay-verify="required" autocomplete="off">
                </div>
            </div>
            <div class="layui-form-item" style="margin-top: 30px">
                <label class="layui-form-label">确认密码</label>
                <div class="layui-input-block">
                    <input id="reNewPwd" name="reNewPwd" type="password" style="float: left;"
                           class="layui-input" placeholder="请再次输入密码"  lay-verify="required" autocomplete="off">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="button" id="savePwd" lay-submit="" lay-filter="savePwd" class="layui-btn" style="width:400px;margin-top:30px; float: right">修&#12288;改
                    </button>
                </div>
            </div>
        </div>

    </form>
</div>
</body>
<script src="../../lib/layui/layui.js"></script>
<script src="../../js/jquery.min.js"></script>
<script src="../../js/application.js"></script>
<script>
    let policeId = GetQueryString("policeId");

    function getUserData(policeId) {
        $("#userInfo").on("load", function () {
            var data = {policeId: policeId}
            this.contentWindow.getParentData(data)
        })
    }
    getUserData(policeId)

    layui.use(['form'], function () {
        var $ = layui.jquery
        var form = layui.form

        //保存按钮执行函数：
        $(function () {
            $("#savePwd").click(function () {
                var policeInfo = JSON.parse(window.sessionStorage.getItem("police"));
                policeId=$("#policeId").val(policeInfo.policeId)
                var oldPwd = document.getElementById("oldPwd").value;
                var newPwd = document.getElementById("newPwd").value;
                var reNewPwd = document.getElementById("reNewPwd").value;
                if(newPwd !==reNewPwd){
                    layer.msg("新密码和确认密码不一致,请保持一致");
                    return false;
                }
                //校验成功，则执行修改密码的后台代码
                if (PasswordCheck1()==PasswordCheck2()) {
                    layer.confirm('确定修改密码?', {icon: 3, title: '提示'}, function (index) {
                        showloading(true)
                        $.ajax({
                            url: mainUri + "/user/updatePwdByP",
                            type: 'post',
                            dataType: 'json',
                            data: {
                                oldPwd: $("#oldPwd").val(),
                                pwd: $("#newPwd").val(),
                                policeId: policeInfo.policeId
                            },
                            success: function (data) {
                                showloading(false)
                                if (data.code == 0) {
                                    $("input").val('');
                                    layer.msg('密码修改成功,请重新登陆', {
                                        icon: 1,
                                        time: 1000
                                    }, function () {
                                        sessionStorage.setItem('tabMenu', '');
                                        sessionStorage.setItem('tabMenuLeft', '');
                                        window.sessionStorage.setItem("police", "");
                                        window.location = mainUri+"/login.html";
                                    });
                                    return false;
                                } else {
                                    layer.alert('密码修改失败了！', {icon: 2});
                                    $("input").val('');
                                    return false;
                                }
                            },
                            error:function(){
                                showloading(false)
                            }
                        });
                    })
                }else{
                    alert("密码输入格式不对，修改密码失败！");
                }
            });
            //鼠标点击密码输入框之外的地方，焦点执行密码校验函数
            $("#newPwd").blur(PasswordCheck1);
            $("#reNewPwd").blur(PasswordCheck2);
        })

        function PasswordCheck1() {
            var Password = document.getElementById("newPwd").value;
            //构建正则表达式，规定可输入的密码为6-12位字符
            var reg_username = /^\w{6,12}$/;
            //执行正则校验，符合返回true，不符合返回false
            var flag = reg_username.test(Password);
            if (flag) {
                //用户名合法
                $("#newPwd").css("border", "");
            } else {
                //用户名非法,加一个红色边框
                $("#newPwd").css("border", "1px solid red");
                layer.msg("新密码不合法，请输入6-12位字符！");

            }
            return flag;
        }
        function PasswordCheck2() {
            var Password = document.getElementById("newPwd").value;
            //构建正则表达式，规定可输入的"密码必须6到12位，且不能出现空格"]
            //  password: [/^[\S]{6,12}$/, "密码必须6到12位，且不能出现空格"],
            var reg_username = /^\w{6,12}$/;
            //执行正则校验，符合返回true，不符合返回false
            var flag = reg_username.test(Password);
            if (flag) {
                //用户名合法
                $("#reNewPwd").css("border", "");
            } else {
                //用户名非法,加一个红色边框
                $("#reNewPwd").css("border", "1px solid red");
                layer.msg("确认密码不合法，请输入6-12位字符！");
            }
            return flag;
        }
    });
</script>
</html>